<?php
namespace app\admin\controller;
use app\admin\model\Admin as AdminModel;
use app\admin\controller\Base;
use think\Session;

class Admin extends Base
{

	//首页展示

    public function index()
    {   
        $auth = new Auth();
        $admin=new AdminModel();
        $adminres=$admin->getadmin();
        foreach ($adminres as $k => $v) {
            $_groupTitle=$auth->getGroups($v['id']);
            $groupTitle=$_groupTitle[0]['title'];
            $groupId=$_groupTitle[0]['group_id'];
            $v['groupTitle']=$groupTitle;
            $v['groupId']=$groupId;
        }
        $this->assign('adminres',$adminres);
        $this->assign('title','管理员首页');
        return view();
    }


    public function add()
    {   
        if(request()->isPost()){
            $data=input('post.');
            $data['create_time'] = time();
            $validate = \think\Loader::validate('Admin');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $admin=new AdminModel();
            if($admin->addadmin($data)){
                $this->success('添加管理员成功！',url('index'));
            }else{
                $this->error('添加管理员失败！');
            }
            return;
        }
        $adminGroupRes=db('admin_group')->select();
        $this->assign('adminGroupRes',$adminGroupRes);
        return view();
    }

    public function edit($id)
    {

        $admins=db('admin')->find($id);

        if(request()->isPost()){
            $data=input('post.');
            $validate = \think\Loader::validate('Admin');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $admin=new AdminModel();
            $savenum=$admin->saveadmin($data,$admins);
            if($savenum == '2'){
                $this->error('管理员用户名不得为空！');
            }
            if($savenum !== false){
                $this->success('修改成功！',url('index'));
            }else{
                $this->error('修改失败！');
            }
            return;
        }
        
        if(!$admins){
            $this->error('该管理员不存在');
        }
        $adminGroupAccess=db('admin_group_access')->where(array('uid'=>$id))->find();
        $adminGroupRes=db('admin_group')->select();
        $this->assign('adminGroupRes',$adminGroupRes);
        $this->assign('admin',$admins);
        $this->assign('groupId',$adminGroupAccess['group_id']);
        return view();
    }

    public function del()
    {
        $admin=new AdminModel();
        require_once 'Geetestlib.php';
        //验证码id key
        define("CAPTCHA_ID", "2d718666555e7b0722379e132e115442");
        define("PRIVATE_KEY", "d23e9a4eda85102c5e88386192b2e5bf");
        //实例化验证码类
        $GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
        // 比如你设置了一个验证码是否验证通过的标识
        $code_flag=false;
        // 这里获取你之前设置的user_id，传送给极验服务器做校验
        $user_id = session('user_id');
        // return $user_id;
        /*$uname = input('post.username');
        // $upass = input('post.password');
        return 2;*/
        if (session('gtserver') == 1) {
          $result = $GtSdk->success_validate(input('geetest_challenge'), input('geetest_validate'), input('geetest_seccode'), $user_id);
          if ($result) {
            // 验证码验证成功
            $code_flag=true;
          } else {
            // 验证码验证失败
            $code_flag=false;
          }
        }else{
          if ($GtSdk->fail_validate(input('geetest_challenge'),input('geetest_validate'),input('geetest_seccode'))) {
             // 验证码验证成功
            $code_flag=true;
          } else {
            // 验证码验证失败
            $code_flag=false;
          }
        }
        // 如果验证码验证成功，再进行其他校验
        if($code_flag){
            if(db('admin')->delete(input('id'))){
                return 1;
            }else{
                return 2;
            }

             
           
        }else{
          // 验证码验证失败，返回2，这里和上面相呼应
          // return $user_id;
          return 2;
        }
        
        
    }
    
    public function yzm()
    {
        require_once 'Geetestlib.php';
        //验证码id key
        define("CAPTCHA_ID", "2d718666555e7b0722379e132e115442");
        define("PRIVATE_KEY", "d23e9a4eda85102c5e88386192b2e5bf");
        //实例化验证码类
        $GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
        // var_dump($GtSdk);
        // die();
        $user_id = "test";
        $status = $GtSdk->pre_process($user_id);
        Session::set('gtserver', $status);
        Session::set('user_id', $user_id);
        echo $GtSdk->get_response_str();
    }
    public function logout()
    {
        session(null);
        $this->success('退出成功！','Login/index');
    }
}
